package com.sxkj.gis.wms.getmap.service.impl;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.servlet.http.HttpSession;
import org.apache.ignite.Ignite;
import org.apache.ignite.Ignition;
import org.opengis.referencing.FactoryException;
import org.opengis.referencing.NoSuchAuthorityCodeException;
import org.springframework.stereotype.Service;

import com.sxkj.gis.wms.bean.DoWmsParams;
import com.sxkj.gis.wms.bean.UrlBean;
import com.sxkj.gis.wms.bean.WmsParams;
import com.sxkj.gis.wms.factory.DoWmsParamsFactory;
import com.sxkj.gis.wms.factory.WmsFactory;
import com.sxkj.gis.wms.getmap.service.WmsGetMapService;

@Service("WmsService")
public class WmsGetMapServiceImpl implements WmsGetMapService {



	public ByteArrayOutputStream WMSgetMapurl(UrlBean url, WmsParams getMapParams, HttpSession session)
			throws NoSuchAuthorityCodeException, FactoryException, IOException {

		boolean conn = (boolean) session.getAttribute("flog");
		WmsFactory wpf = new DoWmsParamsFactory();
		if (conn) {
			String igniteClientGridName = (String) session.getAttribute("clientGridName");
			Ignite ignite = Ignition.ignite(igniteClientGridName);
			System.out.println("--------->目前没有连接Ignite，所有返回为null;");
			return null;
		} else {
			DoWmsParams dwp = wpf.createDoWmsParamsGetMap(getMapParams);
			// getMap由客户端自行处理，并放回
			return dwp.returnPictureByteStream().get("byteArrayOutputStream");
		}

	}

	public ByteArrayOutputStream WMSgetMapxml(UrlBean url, WmsParams getMapParams, HttpSession session)
			throws NoSuchAuthorityCodeException, FactoryException, IOException {
		boolean conn = (boolean) session.getAttribute("flog");
		WmsFactory wpf = new DoWmsParamsFactory();
		if (conn) {
			String igniteClientGridName = (String) session.getAttribute("clientGridName");
			Ignite ignite = Ignition.ignite(igniteClientGridName);
			System.out.println("--------->目前没有连接Ignite，所有返回为null;");
			return null;
		} else {
			// getMap由客户端自行处理，并放回
			DoWmsParams dwp = wpf.createDoWmsParamsGetMap(wpf.createWmsParams(url));//因为url中body属性有参数，所以构建方式不同
			return dwp.returnPictureByteStream().get("byteArrayOutputStream");
		}
	}

	@Override
	public ByteArrayOutputStream WMSgetMapSVG(UrlBean url, WmsParams getMapParams, HttpSession session)
			throws IOException, NoSuchAuthorityCodeException, FactoryException {
		// TODO Auto-generated method stub
		boolean conn = (boolean) session.getAttribute("flog");
		WmsFactory wpf = new DoWmsParamsFactory();
		if (conn) {
			String igniteClientGridName = (String) session.getAttribute("clientGridName");
			Ignite ignite = Ignition.ignite(igniteClientGridName);
			System.out.println("--------->目前没有连接Ignite，所有返回为null;");
			return null;
		} else {
			// getMap由客户端自行处理，并放回
			DoWmsParams dwp = wpf.createDoWmsParamsGetMap(getMapParams);
			return dwp.returnSVGByteStream(dwp.getSVGGenerator()).get("SVG");
		}
	}

	

}
